﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.Framework.Translate;
using CrmContacto.Contracts.Contracts;
using Web.Framework.Mapper;

namespace CrmContacto.WebApp.App
{
    public partial class ProvinciaEdit : BasePage
    {
        public Provincia provinciaCurrent
        {
            get
            {
                return (Provincia)ViewState["Provincia"];
            }
            set
            {
                ViewState["Provincia"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();

        LinkButton eliminarButton = new LinkButton();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton guardarButton = new LinkButton();
            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);

            LinkButton volverButton = new LinkButton();
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.CausesValidation = false;
            volverButton.ID = "BoCommandVolver";
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            volverButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(volverButton);

            eliminarButton.ID = "BoCommandEliminar";
            eliminarButton.CausesValidation = false;
            eliminarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            eliminarButton.Click += new EventHandler(eliminarButton_Click);
            actionButtons.Add(eliminarButton);

            base.LoadContextActions(actionButtons);
        }

        private void eliminarButton_Click(object sender, EventArgs e)
        {
            try
            {
                WebServiceHelper.EliminarProvinciaDto(provinciaCurrent.Id);

                if (!String.IsNullOrEmpty(this.NavigationContext.UrlToGoBack))
                {
                    Response.Redirect(this.NavigationContext.UrlToGoBack + string.Format("&{0}=1", MiscClass.QueryShowDeleted), true);
                }
                else
                {
                    Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPaisEdit + string.Format("?{0}=1", MiscClass.QueryShowDeleted), true);
                }

                //NuevaProvincia(provinciaCurrent.PaisId);
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Message.RegistroEliminado"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
            try
            {
                if (!this.IsPostBack)
                {                    
                    if (this.Request.QueryString["Id"] == null)
                    {
                        int paisId = Convert.ToInt32(this.Request.QueryString["PaisId"]);
                        NuevaProvincia(paisId);
                    }
                    else
                    {
                        var search = new MaestroBaseSearch();
                        search.Id = Convert.ToInt32(this.Request.QueryString["Id"]);
                        provinciaCurrent = WebServiceHelper.GetProvinciasDto(search).First();                        
                    }

                    var pais = WebServiceHelper.GetPaisesDto(new PaisSearch() { Id = provinciaCurrent.PaisId }).First();
                    this.lnkPais.Text = pais.Nombre;

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void NuevaProvincia(int paisId)
        {
            provinciaCurrent = new Provincia();
            provinciaCurrent.PaisId = paisId;
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();

            eliminarButton.Visible = provinciaCurrent.Id != 0;
            if (provinciaCurrent.Id != 0)
            {
                SetJavascriptForDeleteRecord(eliminarButton, provinciaCurrent.Nombre);
            }

            //this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            EntityMapper.Map(provinciaCurrent, this);
        }

        private void SetData()
        {
            EntityMapper.Map(this, provinciaCurrent);
        }

        private void Save()
        {
            provinciaCurrent = WebServiceHelper.SaveProvinciaDto(provinciaCurrent);
            ClearProvincias(HttpContext.Current);            

        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkPais_Click(object sender, EventArgs e)
        {
            try
            {
                if (provinciaCurrent != null)
                {
                    NavigationContext.RedirectTo(MiscClass.UrlPaisEdit, string.Format("?Id={0}", provinciaCurrent.PaisId));
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                NavigationContext.RedirectTo(MiscClass.UrlPaisEdit, string.Format("?Id={0}&{1}=1", provinciaCurrent.PaisId, MiscClass.QueryShowSaveOk));
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Messages.MensajeSaveExitoso"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                if (!String.IsNullOrEmpty(this.NavigationContext.UrlToGoBack))
                {
                    Response.Redirect(this.NavigationContext.UrlToGoBack, true);
                }
                else
                {
                    Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPaisEdit, true);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}